package com.example.main;

import java.util.Calendar;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入年份：");
        while(in.hasNextInt()){
            try{
                int year = in.nextInt();
                System.out.println("请输入月份：");
                int month = in.nextInt() - 1 ;
                printCalendar(year,month);
                System.out.println(System.lineSeparator()+"请输入年份：");
            }catch(InputMismatchException e){
                e.printStackTrace();
            }
        }
    }

    static void printCalendar(int year , int month ) {  //单一功能原则，它是print，就不能包含输入过程。但是可以包含对参数的验证。
        //System.out.println("   日   一  二  三  四   五  六");


        if(month < 0 || month > 11){
            System.out.println("输入的月份有误");
            return;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.set(year,month,1);
        int whichDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        int frontSpace = whichDayOfWeek - 1 ;
        System.out.println("\t日\t一\t二\t三\t四\t五\t六");
        for(int i = 1 ; i <= frontSpace ; i++ ){
            System.out.print("\t　");
        }

        int maxDaysOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        int count = 1 ;
        while(count <= maxDaysOfMonth ){
            System.out.print("\t"+count++);
            if(whichDayOfWeek == 7 || whichDayOfWeek == 0 ){
                System.out.print(System.lineSeparator());
            }
            whichDayOfWeek = (whichDayOfWeek+1) % 7;
        }

    }
}
/*
2025
6


2025
5


2025
4



 */